<?php

include_once 'core/Node.php';

class InputInterpreter{
	
	public function interpret(Node $node){
		$name = $node->getAttribute('#');
		$template = $node->getAttribute('template');
		
		$ret = '<div data-role="fieldcontain">';
		$ret .= $this->$template($node);
		$ret .='</div>';
		return $ret;
	}
	
	function text(Node $node){
		$id = $node->getAttribute('id');
		$dvalue = $node->getAttribute('@1');
		$text = $node->getAttribute('@0');
		
		$ret = '<label for="'.$id.'">'.$text.'</label>';
		$ret .= '<input type="text" name="'.$id.'" id="'.$id.'" value="'.$dvalue.'" style="text-align:right;" />';
		return $ret;		
	}
	
	function select(Node $node){
		$id = $node->getAttribute('id');
		$dvalue = $node->getAttribute('@1');
		$text = $node->getAttribute('@0');
		$options = $node->getChilds();
		$selected = $node->getAttribute('selected');
		
		$ret = '<label for="'.$id.'">'.$text.'</label>';
		$ret .= '<select name="'.$id.'" id="'.$id.'">';
		foreach ($options as $option){
			$val = $option->getAttribute('@0');
			$text = $option->getAttribute('@1');
			
			if($selected == $val){
				$ret .= '<option value="'.$val.'" selected="true">'.$text.'</option>';
			}else{
				$ret .= '<option value="'.$val.'">'.$text.'</option>';
			}
		}
		$ret .= '</select>';
				
		return $ret;
	}
	
	function textarea(Node $node){
		$id = $node->getAttribute('id');
		$text = $node->getAttribute('@0');
		$style = $node->getAttribute('style');
		
		$ret = '<label for="'.$id.'">'.$text.'</label>';
		$ret .= '<textarea name="'.$id.'" id="'.$id.'" style="'.$style.'" >';
		$ret .= '</textarea>';
		return $ret;	
	}
}